﻿#region License Info
//Component of Cronos Package, http://www.codeplex.com/cronos
//Copyright (C) 2009 Anthony Brockwell

//This program is free software; you can redistribute it and/or
//modify it under the terms of the GNU General Public License
//as published by the Free Software Foundation; either version 2
//of the License, or (at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#endregion

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace ExtraControlLibrary.Forms
{
    public partial class TextLister : UserControl
    {
        public event OnSelection SelectionEvent;

        protected virtual void OnSelectionEvent(SelectionEventArgs sea)
        {
            if (SelectionEvent != null)
                SelectionEvent(this, sea);
        }


        private List<string> stringList;
        public List<string> StringList
        {
            get { return stringList; }
            set { stringList = value;
                Refresh(); }
        }

        public int SelectedIndex;

        private int xOffset = 8;
        private int yOffset = 10;

        public TextLister(List<string> theList)
        {
            InitializeComponent();
            StringList = theList;
            SelectedIndex = 0;  // meaning none
        }

        public TextLister()
        {
            InitializeComponent();
            SelectedIndex = -1;  // meaning none
        }

        private List<Rectangle> boxes = null;

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            if (StringList == null)
                return;
            using (Font font = SystemFonts.DefaultFont)
            {
                int h = font.Height+1;

                boxes = new List<Rectangle>();

                for (int i = 0; i < StringList.Count; ++i)
                {
                    var sz = g.MeasureString(StringList[i], font).ToSize();
                    var highlighted = new Rectangle(xOffset - 2, yOffset - 2 + i*h, sz.Width + 4, sz.Height + 4);
                    boxes.Add(highlighted);
                    if (SelectedIndex == i)
                        g.FillRectangle(SystemBrushes.MenuHighlight, highlighted);
                    g.DrawString(StringList[i], font, Brushes.Black, xOffset, h*i + yOffset);
                }
            }
        }

        public Size GetPixelSize()
        {
            var sz = new Size(0, 0);
            Graphics g = CreateGraphics();
            if (StringList == null)
                return sz;
            using (Font font = SystemFonts.DefaultFont)
            {
                int h = font.Height + 1;
                int w = 0;
                for (int i = 0; i < StringList.Count; ++i)
                {
                    var tsz = g.MeasureString(StringList[i], font).ToSize();
                    if (tsz.Width > w)
                        w = tsz.Width;
                }
                sz = new Size(4 + 2*xOffset + w, h*StringList.Count + 2*yOffset + 4);
            }
            return sz;
        }

        private int FoundBox(Point location)
        {
            int foundBox = -1;
            if (boxes == null)
                return foundBox;
            for (int i = 0; i < boxes.Count; ++i)
                if (boxes[i].Contains(location))
                    foundBox = i;
            return foundBox;
        }

        private void TextLister_MouseMove(object sender, MouseEventArgs e)
        {
            // otherwise we may be able to highlight something else
            int foundBox = FoundBox(e.Location);
            if (foundBox != -1)
                if (SelectedIndex != foundBox)
                {
                    SelectedIndex = foundBox;
                    Refresh();
                }
        }

        private void TextLister_MouseEnter(object sender, EventArgs e)
        {
            int i = 0;
            ++i;
        }

        private void TextLister_MouseClick(object sender, MouseEventArgs e)
        {
            // something has been selected
            SelectedIndex = FoundBox(e.Location);

            var sea = new SelectionEventArgs();
            sea.selectedIndex = SelectedIndex;
            OnSelectionEvent(sea);
        }

        #region Event Handling Stuff

        public delegate void OnSelection(object sender, SelectionEventArgs e);

        public class SelectionEventArgs : EventArgs
        {
            public int selectedIndex;
        }

        #endregion

    }
}